package cn.smbms.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.smbms.biz.BillBiz;
import cn.smbms.biz.ProviderBiz;
import cn.smbms.entity.Bill;
import cn.smbms.entity.Provider;
import cn.smbms.entity.User;
import utils.Counts;

@Controller
@RequestMapping("bill")
public class BillControll {
	@Autowired
	private BillBiz billBiz;
	
	
	@RequestMapping("getBillList")
	public String getBillList(Bill bill,Model model) throws Exception {
	
		List<Bill> billList = billBiz.getBillList(bill);
		model.addAttribute("billList", billList);
		model.addAttribute("queryProductName", bill.getProductName());
		model.addAttribute("queryProviderId", bill.getProviderId());
		model.addAttribute("queryIsPayment", bill.getIsPayment());
		
		return "billlist";
	}
	
	@RequestMapping(value="addBill",method=RequestMethod.GET)
	public String toAddBill() {
		return "billadd";
	}
	
	@RequestMapping(value="addBill",method=RequestMethod.POST)
	public String addBill(@Valid @ModelAttribute("bill") Bill bill,BindingResult bindingResult,HttpSession session) throws Exception {
		if(bindingResult.hasErrors()) {
			return "billadd";
		}
		
		User u = (User)session.getAttribute(Counts.USER_SESSION);
		bill.setCreatedBy(u.getId());
		bill.setCreationDate(new Date());
		int n = billBiz.add(bill);
		if(n>0) {
			return "redirect:/bill/getBillList";
		}else {
			return "billadd";
		}
		
	}
	//查看
	@RequestMapping("/viewBill")
	public String viewBill(@RequestParam("billid") Integer id,HttpSession session,Model model) throws Exception {
		Bill bill = billBiz.getBillById(id);
		model.addAttribute("bill", bill);
		return "billview";
	}
	
	//修改
	@RequestMapping(value="/modifyBill",method=RequestMethod.GET)
	public String toModifyBill(@RequestParam("billid") Integer id,Model model) throws Exception {
		Bill bill = billBiz.getBillById(id);
		model.addAttribute("bill", bill);
		return "billmodify";
	}
	
	//修改
	@RequestMapping(value="/modifyBill",method=RequestMethod.POST)
	public String doModifyBill(Bill bill,Model model,HttpSession session) throws Exception {
		User u = (User)session.getAttribute(Counts.USER_SESSION);
		bill.setCreatedBy(u.getId());
		bill.setCreationDate(new Date());
		int n = billBiz.modify(bill);
		if(n>0) {
			return "redirect:/bill/getBillList";
		}else {
			model.addAttribute("bill", bill);
			return "billmodify";
		}
		
	}
	
	@RequestMapping("/deleteBill")
	public String deleteBill(@RequestParam("billid") Integer id,HttpServletRequest request) throws Exception {
		int n = billBiz.deleteBillById(id);
		if(n>0) {
			request.setAttribute("msg", "删除成功");
		}else {
			request.setAttribute("msg", "删除失败");
		}
		return "redirect:/bill/getBillList";
	}
}
